1 using UnityEngine;
2 using
System.Collections;
3
4 public
class JumpAndRunMovement : MonoBehaviour
5 {
6     
public float Speed;
7     
public float JumpForce;
8
9     Animator m_Animator;
10     Rigidbody2D m_Body;
11     PhotonView m_PhotonView;
12
13     
bool m_IsGrounded;
14
15     
void Awake()
16     {
17         m_Animator = GetComponent<Animator>();
18         m_Body = GetComponent<Rigidbody2D>();
19         m_PhotonView = GetComponent<PhotonView>();
20     }
21
22     
void Update()
23     {
24         UpdateIsGrounded();
25         UpdateIsRunning();
26         UpdateFacingDirection();
27     }
28
29     
void FixedUpdate()
30     {
31         
if( m_PhotonView.isMine == false )
32         {
33             
return;
34         }
35
36         UpdateMovement();
37         UpdateJumping();
38     }
39
40     
void UpdateFacingDirection()
41     {
42         
if( m_Body.velocity.x > 0.2f )
43         {
44             transform.localScale =
new Vector3( 1, 1, 1 );
45         }
46         
else if( m_Body.velocity.x < -0.2f )
47         {
48             transform.localScale =
new Vector3( -1, 1, 1 );
49         }
50     }
51
52     
void UpdateJumping()
53     {
54         
if( Input.GetKey( KeyCode.Space ) == true && m_IsGrounded == true )
55         {
56             m_Animator.SetTrigger(
"IsJumping" );
57             m_Body.AddForce( Vector2.up * JumpForce );
58             m_PhotonView.RPC(
"DoJump", PhotonTargets.Others );
59         }
60     }
61
62     
[RPC]
63     
void DoJump()
64     {
65         m_Animator.SetTrigger(
"IsJumping" );
66     }
67
68     
void UpdateMovement()
69     {
70         Vector2 movementVelocity = m_Body.velocity;
71
72         
if( Input.GetAxisRaw( "Horizontal" ) > 0.5f )
73         {
74             movementVelocity.x = Speed;
75             
76         }
77         
else if( Input.GetAxisRaw( "Horizontal" ) < -0.5f )
78         {
79             movementVelocity.x = -Speed;
80         }
81         
else
82         {
83             movementVelocity.x =
0;
84         }
85
86         m_Body.velocity = movementVelocity;
87     }
88
89     
void UpdateIsRunning()
90     {
91         m_Animator.SetBool(
"IsRunning", Mathf.Abs( m_Body.velocity.x ) > 0.1f );
92     }
93
94     
void UpdateIsGrounded()
95     {
96         Vector2 position =
new Vector2( transform.position.x, transform.position.y );
97
98         RaycastHit2D hit = Physics2D.Raycast( position, -Vector2.up,
0.1f, 1 << LayerMask.NameToLayer( "Ground" ) );
99
100         m_IsGrounded = hit.collider !=
null;
101         m_Animator.SetBool(
"IsGrounded", m_IsGrounded );
102     }
103 }



Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.460 lượt xem

Gõ tìm kiếm nhanh...